package com.example.demo;

import com.google.gson.JsonElement;
import microsoft.aspnet.signalr.client.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import microsoft.aspnet.signalr.client.hubs.HubConnection;
import microsoft.aspnet.signalr.client.hubs.HubProxy;
import java.util.Scanner;

//this is master branch
//@SpringBootApplication
public class DemoApplication {
    //this is master
    public static void main(String[] args) {
        Logger logger = new Logger() {

            @Override
            public void log(String message, LogLevel level) {
                System.out.println(message);
            }
        };

        HubConnection conn = new HubConnection("http://www.bugong.com:80/", "", true, logger);
        HubProxy  proxy = conn.createHubProxy("myHub");

        proxy.subscribe(new Object() {
            @SuppressWarnings("unused")
            public void messageReceived(String name, String message) {
                System.out.println(name + ": " + message);
            }
        });
        conn.error(new ErrorCallback() {

            @Override
            public void onError(Throwable error) {
                error.printStackTrace();
            }
        });

        // Subscribe to the connected event
        conn.connected(new Runnable() {

            @Override
            public void run() {
                System.out.println("CONNECTED");
            }
        });

        // Subscribe to the closed event
        conn.closed(new Runnable() {

            @Override
            public void run() {
                System.out.println("DISCONNECTED");
            }
        });


        conn.received(new MessageReceivedHandler() {

            @Override
            public void onMessageReceived(JsonElement json) {
                System.out.println("RAW received message: " + json.toString());
            }
        });
//        SignalRFuture<Void> awaitConnection = conn.start();
//
//        try {
//            awaitConnection.get();
//        }
//        catch (Exception ex) {
//            System.out.println(ex.getMessage());
//        }
        conn.start()
                .done(new Action<Void>() {

                    @Override
                    public void run(Void obj) throws Exception {
                        System.out.println("Done Connecting!");
                    }
                });
        String name = "javauser";
        String msg="javamsg";
        Scanner inputReader = new Scanner(System.in);

        String line = inputReader.nextLine();
        while (!"exit".equals(line)) {
            proxy.invoke("Send", name, line).done(new Action<Void>() {

                @Override
                public void run(Void obj) throws Exception {
                    System.out.println("SENT!");
                }
            });

            line = inputReader.next();
        }
        inputReader.close();

        conn.stop();
//        SpringApplication.run(DemoApplication.class, args);
    }
}
